%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% PLOT PREDICTED SHARE OF DEFORESTATION VS TRANSPORTATION COSTS

% "DEFORESTATION IN THE AMAZON:
% A UNIFIED FRAMEWORK FOR ESTIMATION AND POLICY ANALYSIS"

% by Eduardo Souza-Rodrigues

% This version: November 2018

% OBSERVATIONS: 
% 1. Run this program after running "dem_def_reg.m"
% 2. The data set has to be loaded before running this program.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% COMPUTE PREDICTED DEFORESTATION AT EVALUATION POINTS

% I. Evaluation points
% Mean of X
x_bar = mean([X ones(T,1)]);

% Transportation Costs
EVTC  = linspace(min(TC),max(TC),1001)';

% II. Predicted Deforestation at Evaluation Points

for p = 1:size(EVTC,1)    % fix the evaluation point
    
    % Predicted quantile - Not sorted
    q_ivqr(p,:) = exp(x_bar * beta_ivqr(2:dxc,:) + EVTC(p)*beta_ivqr(1,:))...
        ./(1 +  exp(x_bar * beta_ivqr(2:dxc,:) + EVTC(p)*beta_ivqr(1,:)));
    
    % Sort predicted quantiles and keep index of original order
    [q , ~] = sort(q_ivqr(p,:));
    % Predicted quantile - sorted
    h_ivqr(p,:) = q;      
            
end

%% PLOT RESULTS


if farm == 1   
    
    quantile_small = figure('visible','off');
    scatter(TC,Yt,'.b')
    xlabel('Transportation Cost to Port (US$/ton)','fontsize',17)
    ylabel('Proportion of Deforestation','fontsize',17)
    title(' a) SMALL FARMS','fontsize',17)
    axis([0 170 0 1])
    hold on
    
    p_ivqr = h_ivqr(:,[90 80 70 60 50 40 30 20 10]);
    p1 = plot(EVTC,p_ivqr);
    legend(p1,'90th','80th','70th','60th','50th','40th','30th','20th','10th','Location','Southwest')
    hold off 
    saveas(quantile_small,'quantile_small.png')
    saveas(quantile_small,'quantile_small.fig','fig')
    
elseif farm == 2
    
    quantile_smallmedium = figure('visible','off');
    scatter(TC,Yt,'.b')
    xlabel('Transportation Cost to Port (US$/ton)','fontsize',17)
    ylabel('Proportion of Deforestation','fontsize',17)
    title(' b) SMALL-MEDIUM FARMS','fontsize',17)
    axis([0 170 0 1])
    hold on
    
    p_ivqr = h_ivqr(:,[90 80 70 60 50 40 30 20 10]);
    p1 = plot(EVTC,p_ivqr);
    legend(p1,'90th','80th','70th','60th','50th','40th','30th','20th','10th','Location','Southwest')
    hold off  
    saveas(quantile_smallmedium,'quantile_smallmedium.png')
    saveas(quantile_smallmedium,'quantile_smallmedium.fig','fig')
    
    
elseif farm == 3
    
    quantile_mediumlarge = figure('visible','off');
    scatter(TC,Yt,'.b')
    xlabel('Transportation Cost to Port (US$/ton)','fontsize',17)
    ylabel('Proportion of Deforestation','fontsize',17)
    title(' c) MEDIUM-LARGE FARMS','fontsize',17)
    axis([0 170 0 1])
    hold on
    
    p_ivqr = h_ivqr(:,[90 80 70 60 50 40 30 20 10]);
    p1 = plot(EVTC,p_ivqr);
    legend(p1,'90th','80th','70th','60th','50th','40th','30th','20th','10th','Location','Southwest')
    hold off  
    saveas(quantile_mediumlarge,'quantile_mediumlarge.png')
    saveas(quantile_mediumlarge,'quantile_mediumlarge.fig','fig')

elseif farm == 4
       
    quantile_large = figure('visible','off');
    scatter(TC,Yt,'.b');
    xlabel('Transportation Cost to Port (US$/ton)','fontsize',17)
    ylabel('Proportion of Deforestation','fontsize',17)
    title(' d) LARGE FARMS','fontsize',17)
    axis([0 170 0 1])
    hold on
    
    p_ivqr = h_ivqr(:,[90 80 70 60 50 40 30 20 10]);
    p1 = plot(EVTC,p_ivqr);
    legend(p1,'90th','80th','70th','60th','50th','40th','30th','20th','10th','Location','Southwest')
    hold off  
    saveas(quantile_large,'quantile_large.png')
    saveas(quantile_large,'quantile_large.fig','fig')
           
end

% Clear
%clear x_bar EVTC
%clear h_ols h_iv h_qr h_ivqr p q index q_qr q_ivqr
%clear p1 p_ivqr

